package top.huhuiyu.spirngboot2.total.ds;

public class MyQueue<T> {
    private Node<T> head;
    private Node<T> tail;
    private int count = 0;

    public boolean isEmpty() {
        return count == 0;
    }

    public int size() {
        return count;
    }

    public void enqueue(T data) {
        Node<T> node = new Node<>();
        node.setData(data);
        if (count == 0) {
            head = node;
        } else {
            tail.setNext(node);
        }
        tail = node;
        count++;
    }

    public T dequeue() {
        T data = head.getData();
        head = head.getNext();
        count--;
        if (count == 0) {
            tail = null;
        }
        return data;
    }

    public T peek() {
        return head == null ? null : head.getData();
    }

}
